package com.wewins.fota.bean.po;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
public class UpdatePlan {
    private int id;

    @Size(min = 2, max = 30, message = "升级计划名称长度[2,30]")
    private String name;

    @NotBlank
    private String product;

    private String cond;

    // version1,version2,...
    @NotBlank
    private String srcV;

    // version3
    @NotBlank
    private String dstV;

    private long createTime;

    private long updateTime;

    private String description;

    private int status = -1;

    public List<String> parseSrcVersions() {
        return Stream.of(this.srcV.split(",")).collect(Collectors.toList());
    }

    public UpdatePlan refreshUser(String username) {
        if (!StringUtils.isEmpty(this.cond)) {
            Map<String, String> tmp = JSONObject.parseObject(this.cond, Map.class);
            tmp.put("owner", username);
            this.cond = JSONObject.toJSONString(tmp);
        }
        return this;
    }
}
